<template>
	<div id="user_info">
		<h3 class="text-info">用户信息</h3>
		<table class="table">
			<tr>
				<th>用户名</th>
				<th>昵称</th>
				<th>邮箱</th>
				<th>身份</th>
				<th>发帖数</th>
				<th>评论数</th>
				<th>操作</th>
			</tr>
			<tr v-for="u in user" :key="u.id">
				<td>{{u.username}}</td>
				<td>{{u.name}}</td>
				<td>{{u.mail}}</td>
				<td>{{u.ide}}</td>
				<td>{{u.countPost}}</td>
				<td>{{u.countComment}}</td>
				<td><button @click="deleteUser(u.id)" class="btn btn-danger">删除</button></td>
			</tr>

		</table>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				userP: {},
				userC: {},
				user: {}
			}
		},
		created() {
			this.getAllUser();
		},
		methods: {
			getAllUser() {
				this.$http.get('/api/getAllUserP').then(data => {
					if(data.status == 200) {
						this.userP = data.body;
						//						if(this.userC != {})
						//							this.mergeUser();
					}
				}, err => {
					console.log(err)
				})

				this.$http.get('/api/getAllUserC').then(data => {
					if(data.status == 200) {
						this.userC = data.body;
						if(this.userP != {})
							this.mergeUser();
					}
				}, err => {
					console.log(err)
				})

			},
			mergeUser() {
				this.user = this.userP;
				for(var i = 0; i < this.userP.length; i++) {
					for(var j = 0; j < this.userC.length; j++) {
						if(this.userP[i].id == this.userC[j].id) {

							this.user[i].countComment = this.userC[j].countComment
						}
						if(this.userP[i].level == 0) {
							this.user[i].ide = "用户";
						}
						if(this.userP[i].level == 1) {
							this.user[i].ide = "版主";
						}
						if(this.userP[i].level == 2) {
							this.user[i].ide = "管理员";
						}
					}
				}
//				console.log(this.user);
			},

			deleteUser(userId) {
				var sure = confirm("确定删除？")
				if(sure == true) {
					this.$http.get('/api/deleteUser', {
						params: {
							id: userId
						}
					}).then(data => {
						if(data.status == 200) {
							//						console.log("删除成功");
							this.getAllUser();
						}
					}, err => {
						console.log(err)
					})
				}
			}
		}
	}
</script>

<style scoped>
	#user_info {
		width: 700px;
		margin:0 auto
	}
</style>